import { reqTask, reqMapsInfoByPictureId, reqSelectTask } from '@/service/taskmanage';
import { displayIframe } from '@/service/maps'
import router from 'umi/router';

const Model = {
    namespace: 'taskmanage',
    state: {
        taskList: [],
        total: 0,
        contrastIframe: ''
    },
    effects: {
        * reqTask({ payload }, { put, call }) {
            const res = yield call(reqTask, payload)
            if (res.code === 200) {
                yield put({
                    type: 'saveTaskList',
                    payload: res.raw
                })
                yield put({
                    type: 'saveTotal',
                    payload: res.total
                })
            }
        },
        * reqMapsInfoToTask({ payload }, { put, call }) {
            const res = yield call(reqMapsInfoByPictureId, payload)
            if (res.code === 200) {
                yield put({
                    type: 'maps/saveMapsInfo',
                    payload: res.raw
                })
                yield put({
                    type: 'maps/changeMapsId',
                    payload: res.raw[0].maps.sampleId
                })
                router.push({
                    pathname: '/work/task',
                    search: `${res.raw[0].maps.sampleId}/1`
                })
            }
        },
        * reqMineInfoToTask({ payload }, { put, call }) {
            const res = yield call(reqMapsInfoByPictureId, payload)
            if (res.code === 200) {
                yield put({
                    type: 'maps/saveMapsInfo',
                    payload: res.raw
                })
                yield put({
                    type: 'maps/changeMapsId',
                    payload: res.raw[0].maps.sampleId
                })
                router.push({
                    pathname: '/work/task',
                    search: `${res.raw[0].maps.sampleId}/2`
                })
            }
        },
        * reqSelectTask({ payload }, { put, call }) {
            const res = yield call(reqSelectTask, payload)
            yield put({
                type: 'saveTaskList',
                payload: res.raw
            })
            yield put({
                type: 'saveTotal',
                payload: res.total
            })
        },
        * mapsContrast({ payload }, { put, call }) {
            const res = yield call(displayIframe, payload)
            console.log('contrastRes', res)
            if (res.code == 200) {
                yield put({
                    type: 'saveContrastIframe',
                    payload: res.path
                })
            }
        }
    },
    reducers: {
        saveTaskList(state, { payload }) {
            return {...state, taskList: payload }
        },
        saveTotal(state, { payload }) {
            return {...state, total: payload }
        },
        saveContrastIframe(state, { payload }) {
            return {...state, contrastIframe: payload }
        }
    }
}
export default Model